package com.cskaoyan.gateway.controller.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderListService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.CreateOrderRequest;
import com.mall.order.dto.CreateOrderResponse;
import com.mall.order.dto.OrderListRequest;
import com.mall.order.dto.OrderListResponse;
import com.mall.user.util.IJwtTokenUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @author pengyehua
 * @date 2020/12/20 22:05
 */

/**
 * 订单模块
 */
@RestController
public class OrderController {

    @Reference(check = false)
    OrderCoreService orderCoreService;

    @Reference(check = false)
    OrderListService orderListService;

    @Reference(check = false)
    IJwtTokenUtils jwtTokenUtils;


    @GetMapping("shopping/order")
    public ResponseData orderList(Integer page, Integer size, String sort, HttpServletRequest request) {
//        Integer page =(Integer) map.get("page");
//        Integer size =(Integer) map.get("size");
//        String sort = (String) map.get("sort");
        String access_token = CookieUtil.getCookieValue(request, "access_token");
        OrderListRequest orderListRequest = new OrderListRequest();
        orderListRequest.setPage(page);
        orderListRequest.setSize(size);
        orderListRequest.setSort(sort);

        //token解密
        Long uid = getUidByToken(access_token);
        orderListRequest.setUserId(uid);
        OrderListResponse response = orderListService.queryOrderDetailByUserId(orderListRequest);
        if (!response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(response.getMsg());
        }
        return new ResponseUtil().setData(response);
    }

    // 用户下单请求
    @PostMapping("shopping/order")
    public ResponseData orderCreate(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest) {


        //从cookie获取access_token，解析获取userId
        String access_token = CookieUtil.getCookieValue(servletRequest, "access_token");
        Long uid = getUidByToken(access_token);

        //设置id和唯一key

        request.setUserId(uid);
        request.setUniqueKey(UUID.randomUUID().toString());

        CreateOrderResponse response = orderCoreService.createOrder(request);

        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getOrderId());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());

    }

    /**
     * 通过token获取用户id
     *
     * @param access_token
     * @return
     */
    private Long getUidByToken(String access_token) {
        jwtTokenUtils.setToken(access_token);
        String userTokenJson = jwtTokenUtils.freeJwt();
        System.out.println(userTokenJson);
        JSONObject jsonObject = JSON.parseObject(userTokenJson);
        long uid = Long.parseLong(jsonObject.getString("uid"));
        return uid;
    }
}
